#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
检查数据库表结构和数据
"""
import sys
import os
sys.path.insert(0, os.path.abspath('.'))

from backend import create_app
from backend.models.user import User
from backend.models.notification import Notification
from backend.infrastructure.database import db
from sqlalchemy import inspect

app = create_app()

with app.app_context():
    print("=== 数据库表结构检查 ===")
    
    # 检查notifications表结构
    inspector = inspect(db.engine)
    columns = inspector.get_columns('notifications')
    
    print("notifications表字段:")
    for col in columns:
        print(f"  - {col['name']}: {col['type']}")
    
    print("\n=== 用户数据 ===")
    users = User.query.all()
    print(f"总用户数: {len(users)}")
    
    for user in users:
        roles = [role.name for role in user.roles] if user.roles else []
        print(f"用户ID: {user.id}, 用户名: {user.username}, 角色: {roles}")
    
    print("\n=== 通知数据 ===")
    notifications = Notification.query.all()
    print(f"总通知数: {len(notifications)}")
    
    for notification in notifications:
        print(f"ID: {notification.id}, 标题: {notification.title}, 状态: {notification.status}, 用户ID: {notification.user_id}, 目标: {notification.target_users}")
    
    print("\n=== 角色查询测试 ===")
    
    admin_users = User.query.filter(User.roles.any(name='admin')).all()
    print(f"管理员用户数: {len(admin_users)}")
    
    doctor_users = User.query.filter(User.roles.any(name='doctor')).all()
    print(f"医生用户数: {len(doctor_users)}")
    
    parent_users = User.query.filter(User.roles.any(name='parent')).all()
    print(f"家长用户数: {len(parent_users)}")
